﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


/**
 * Make sure that you have added references to Visifire assembly, 
 * WPFVisifire.Charts.dll. Do not forget to include the Visifire.Charts 
 * and Visifire.Commons within any code file where you want to work 
 * with the charts. These files can be added with following syntax:
 * */
using Visifire.Charts;
using Visifire.Commons;

using System.Collections.ObjectModel;

using GasData;

using System.Threading;

// CancelEventArgs
using System.ComponentModel;

namespace WPF_DataLogger
{
    /// <summary>
    /// Interaction logic for DataPresent.xaml
    /// </summary>
    public partial class DataPresent : Window
    {
        public DataPresent()
        {
            InitializeComponent();

            chart_gas_data_table = new Dictionary<String, GasInfo>();

            gas_chart_ui_data = new ObservableCollection<GasInfo>();

            GasChart.DataContext = gas_chart_ui_data;
        }


        public void DataPresent_Closing(object sender, CancelEventArgs e)
        {
            GasChart.DataContext = null;
            chart_gas_data_table.Clear();
            gas_chart_ui_data.Clear();
        }

        /*The channel to receive data from the data logger */
        public void GasDataReceiving(object sender, GasDataUpdateEventArgs gas_data)
        {
            if (gas_data.Gas_data_table.Count == 0)
            {
                MessageBox.Show("DataPresent:No data received yet.", "No data",
                    MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            foreach (KeyValuePair <String, GasInfo> gas_item in gas_data.Gas_data_table)
            {
                if (!chart_gas_data_table.ContainsKey(gas_item.Key))
                {
                    chart_gas_data_table.Add(gas_item.Key, gas_item.Value);
                }
                else
                {
                    chart_gas_data_table[gas_item.Key].UpdateGasInfo(gas_item.Value.Cur_val);                   
                }
            }

            UpdateGasChart();        
        }


        /*****************************************************
         * Function:
         * Summary:
         * Input:
         * Returns:
         * Side effects:
         * Pre-Conditions:
        ******************************************************/
        private void UpdateGasChart()
        {
            gas_chart_ui_data.Clear();

            foreach (KeyValuePair<String, GasInfo> gas_item in chart_gas_data_table)
            {
                gas_chart_ui_data.Add(new GasInfo(gas_item.Value));
            }
            
        }

        /*****************************************************
         * Function:
         * Summary:
         * Input:
         * Returns:
         * Side effects:
         * Pre-Conditions:
        ******************************************************/
        private void CreateGasDataChart()
        {
            // Create a Chart element
            Chart chart = new Chart();

            // Set chart width and height
            chart.Width = 400;
            chart.Height = 300;

            // Create new DataSeries
            DataSeries dataSeries = new DataSeries();

            // Number of DataPoints to be generated
            int numberOfDataPoints = chart_gas_data_table.Count;

            // To set the YValues of DataPoint
            Random random = new Random();

            // Loop and add a few DataPoints
            foreach (KeyValuePair<String, GasInfo> gas_item in chart_gas_data_table)
            {
                // Create a DataPoint
                DataPoint dataPoint = new DataPoint();

                // Set the YValue using random number
                dataPoint.YValue = gas_item.Value.Cur_val;

                // Add DataPoint to DataSeries
                dataSeries.DataPoints.Add(dataPoint);
            }

            // Add DataSeries to Chart
            chart.Series.Add(dataSeries);

            // Add chart to the LayoutRoot for display
            LayoutRoot.Children.Add(chart);
        }


        /*****************************************************
         *                  Internal Data
        ******************************************************/

        Dictionary<String, GasInfo> chart_gas_data_table;
        ObservableCollection<GasInfo> gas_chart_ui_data;
    }

}
